<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\users;

use think\facade\Db;
use app\common\dao\users\UsersMessageDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class UsersMessageRepository
 *
 * @mixin UsersMessageDao
 */
class UsersMessageRepository extends BaseRepository
{

    public function __construct(UsersMessageDao $dao)
    {
        $this->dao = $dao;
    }

    public function getMessageType(int $id = null)
    {
        $data = [
            'bill' => '账单消息',
            'system' => '系统消息',
            'contract' => '合同消息',
            'property' => '物业消息',
            'energy_meter' => '能源消息',
            'repair_order' => '工单消息',
        ];
        if ($id) {
            return $data[$id] ?? '';
        }
        return $data;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }


    /**
     * @param int $propertyId 宿舍 ID
     * @param int $trackType 操作端口 1 管理后台 2 企业后台 3 宿舍后台 4 用户中心 5 移动端
     * @param int $userId USER 用户表 ID
     * @param int $isType 关联数据类型 1 宿舍 2 房间 3 合同 4账单 5 能耗
     * @param int $isClass 关联操作类型  1 添加 2 编辑 3 删除 4 查看 5 充值 6 审核 7 催缴 8收款 9 其它
     * @param int $contractId 合同 ID
     * @param int $roomId 房间 ID
     * @param int $billId 账单 ID
     * @param string $remark 备注说明
     * @return void
     */
    public function addLog(int $propertyId, int $userId, $messageType, array $messageData = null)
    {
        $data['status'] = 1;
        $data['user_id'] = $userId;
        $data['property_id'] = $propertyId;
        $data['message_type'] = $messageType;
        if (isset($messageData['message_title'])) {
            $data['message_title'] = $messageData['message_title'];
        }
        if (isset($messageData['content'])) {
            $data['content'] = $messageData['content'];
        }
        $this->dao->create($data);
    }

    public function addInfo(array $data, int $propertyId = 1, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $res = $this->dao->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'USERS.MESSAGE.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'USERS.MESSAGE.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }


    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->where('id', $id)
            ->find();
        return $data;
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'USERS.MESSAGE.DEL', $info);
        $this->dao->delete($info['id']);
    }

}